package com.pg.agent.engine.service.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * websocket连接管理类
 */
public class SessionConnectManager {

    private static final Logger logger = LoggerFactory.getLogger(SessionConnectManager.class);
    /**
     * 缓存当前连接信息,如果存在场景则key为sceneName+"_"+userId，否则key为userId
     */
    private final ConcurrentHashMap<String, AtomicBoolean> storyMap = new ConcurrentHashMap<>();

    private final ConcurrentHashMap<String, AtomicBoolean> stopMap = new ConcurrentHashMap<>();

    private static SessionConnectManager instance = new SessionConnectManager();

    public static SessionConnectManager getInstance() {
        return instance;
    }


    public void addSessionId(String sessionId) {
        if (storyMap.get(sessionId) == null) {
            AtomicBoolean atomicBoolean = new AtomicBoolean();
            atomicBoolean.set(true);
            storyMap.put(sessionId, atomicBoolean);
        }
    }

    public void removeStorySessionId(String sessionId) {
        storyMap.remove(sessionId);
    }

    public void removeStopSessionId(String sessionId) {
        stopMap.remove(sessionId);
    }
    public void addStopSessionId(String sessionId) {
        if (stopMap.get(sessionId) == null) {
            AtomicBoolean atomicBoolean = new AtomicBoolean();
            atomicBoolean.set(true);
            stopMap.put(sessionId, atomicBoolean);
        }
    }

    public ConcurrentHashMap<String, AtomicBoolean> getStoryMap() {
        return storyMap;
    }

    public ConcurrentHashMap<String, AtomicBoolean> getStopMap() {
        return stopMap;
    }
}

